package custom;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Extends Animator with capability to draw a bouncing balls model.
 * 
 * This class can be left unmodified for the bouncing balls lab. :)
 * 
 * @author Oscar Soderlund
 * 
 */
@SuppressWarnings("serial")
public final class BouncingBalls extends Animator {

	private static final double PIXELS_PER_METER = 20;

	private IBouncingBallsModel model, model2;
	private double modelHeight;
	private double deltaT;
	
	private BallCollection BallList = new BallCollection();
	
	@Override
	public void init() {
		super.init();
		double modelWidth = canvasWidth / PIXELS_PER_METER;
		modelHeight = canvasHeight / PIXELS_PER_METER;
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= 1; j++) {
				BallList.add(new Ball(modelWidth, modelHeight, i, j, Math.random()*1.4));
			}
		}
//		BallList.add(new Ball(modelWidth, modelHeight, 1, 1, 1));
//		BallList.add(new Ball(modelWidth, modelHeight, 3, 5, 1.2));
//		BallList.add(new Ball(modelWidth, modelHeight, 8, 8, 1));
//		BallList.add(new Ball(modelWidth, modelHeight, 2, 8, 1));
//		BallList.add(new Ball(modelWidth, modelHeight, 8, 2, 1));
	}

	@Override
	protected void drawFrame(Graphics2D g) {
		// Clear the canvas
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, canvasWidth, canvasHeight);
		// Transform balls to fit canvas
		g.setColor(Color.BLUE);
		g.scale(PIXELS_PER_METER, -PIXELS_PER_METER);
		g.translate(0, -modelHeight);
		// Update and draw Balls
		BallList.updateInformation(deltaT);
		for (Ball b : BallList) {
			g.fill(b.getBallEllipse2D());
		}
	}

	@Override
	protected void setFrameRate(double fps) {
		super.setFrameRate(fps);
		// Update deltaT according to new frame rate
		deltaT = 1 / fps;
	}
}